{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "89ed73a7-9803-4b99-a952-62b54018e299",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6dc554b4-441f-4b69-bc41-a6cad4e777ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "from IPython.display import Markdown, display\n",
    "from llama_index import SimpleDirectoryReader, StorageContext, VectorStoreIndex\n",
    "openai.api_key =\"\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ab97500f-c78a-4d8a-a8d0-0752c9b44d61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Document ID: 1 8d84aefd-ca73-4c1e-b83d-141c1b1b3ba6\n"
     ]
    }
   ],
   "source": [
    "!mkdir -p 'data/paul_graham/'\n",
    "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt'\n",
    "# load documents\n",
    "documents = SimpleDirectoryReader(\"./data/paul_graham/\").load_data()\n",
    "print(\"Document ID:\", len(documents),documents[0].doc_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "edefa044-3c33-4074-9042-b419660a72ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8d48dc2898354e5ca08dc41f3693d415",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/7 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from llama_index import ServiceContext\n",
    "from llama_index.embeddings import HuggingFaceEmbedding\n",
    "from llama_index.vector_stores import VearchVectorStore\n",
    "\n",
    "\"\"\"\n",
    "vearch cluster\n",
    "\"\"\"\n",
    "vector_store = VearchVectorStore( \n",
    "    path_or_url = 'http://liama-index-router.vectorbase.svc.sq01.n.jd.local',\n",
    "    table_name = 'liama_index_test2',\n",
    "    db_name = 'liama_index',\n",
    "    flag = 1)\n",
    "\n",
    "\"\"\"\n",
    "vearch standalone\n",
    "\"\"\"\n",
    "# vector_store = VearchVectorStore(         \n",
    "#         path_or_url = '/data/zhx/zhx/liama_index/knowledge_base/liama_index_teststandalone',\n",
    "#         # path_or_url = 'http://liama-index-router.vectorbase.svc.sq01.n.jd.local',\n",
    "#         table_name = 'liama_index_teststandalone',\n",
    "#         db_name = 'liama_index',\n",
    "#         flag = 0)\n",
    "\n",
    "embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")\n",
    "service_context = ServiceContext.from_defaults(embed_model=embed_model)\n",
    "storage_context = StorageContext.from_defaults(vector_store=vector_store)\n",
    "index = VectorStoreIndex.from_documents(\n",
    "    documents, storage_context=storage_context, service_context=service_context\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c4ad5c36-95c4-49b1-8517-9d9161ba18d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The author did not provide any information about their growing up.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"What did the author do growing up?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2f1bb76f-609b-4467-9411-b33ceb7356b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>The author wrote all of Y Combinator's internal software in Arc while continuing to work on Y Combinator, but later stopped working on Arc and focused on writing essays and working on Y Combinator. In 2012, the author's mother had a stroke, and the author realized that Y Combinator was taking up too much of their time and decided to hand it over to someone else. The author suggested this to Robert Morris, who offered unsolicited advice to the author to make sure Y Combinator wasn't the last cool thing the author did. The author ultimately decided to hand over the leadership of Y Combinator to Sam Altman in 2013.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\n",
    "    \"What did the author do after his time at Y Combinator?\"\n",
    ")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc9c7d87-b192-4abb-9099-a2875c1a6963",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
